const express = require('express');
const router = express.Router();

const home = require('./routes/home');
const buller = require('./routes/buller');
const seller = require('./routes/seller');
const good = require('./routes/good');
const message = require('./routes/message');

const multer = require('multer');

//********系统PC端  管理员*****************
router.get('/guanli',home.index);
router.get('/login',home.login);
router.get('/regist',home.regist);
router.get('/buller',home.buller);
router.get('/seller',home.seller);
router.get('/login',home.login);
router.get('/goods',home.goods);

router.post('/regist',home.postRegist);
router.post('/login',home.postLogin);
router.get('/disBuller',home.disBuller);
router.get('/disSeller',home.disSeller);
router.get('/removeGood',home.removeGood);
router.get('/guanli/clearDatabase',home.clearDatabase);


let storage = multer.diskStorage({
    destination: 'public/images',

    filename: (req, file, cb) =>{

        if ( req.body.xu instanceof Array){
            req.body.xu = req.body.xu[0]-0;
        }else{
            req.body.xu = req.body.xu-0;
        }
        var a = Date.now();


        var ava = '/images/'+req.session.seller._id+req.body.name + '-' +(req.body.xu++)+'-'+ a +'.jpg,';

        cb(null,req.session.seller._id+req.body.name + '-' +(req.body.xu-1)+'-'+ a +'.jpg');
        req.body.avatar += ava;

    }
});
let upl = multer({
    storage: storage
});

let stor = multer.diskStorage({
    destination: 'public/images/user',

    filename: (req, file, cb) =>{


        cb(null,req.session.seller._id +'.jpg');
        req.body.avatar ='/images/user/'+ req.session.seller._id +'.jpg';

    }
});
let upl1 = multer({
    storage: stor
});






//*************卖家**************

router.post('/seller/regist',seller.regist);
router.post('/seller/login',seller.login);
router.get('/seller/checkLogin',seller.checkLogin);
router.get('/seller/out',seller.out);
router.post('/seller/addGoods',upl.array('photo',10),seller.addGoods);
router.post('/seller/changeInfo',upl1.single('photo'),seller.changeInfo);
router.get('/seller/myGoods',seller.myGoods);
router.get('/seller/sellerGoods',seller.sellerGoods);
router.get('/seller/remove',seller.remove);
router.get('/seller/change',seller.change);
router.get('/seller/checkNewGood',seller.checkNewGood);
router.post('/seller/changeForm',seller.changeForm);
router.get('/seller/like',seller.like);
router.get('/seller/getInfo',seller.getInfo);
router.get('/seller/noSend',seller.noSend);
router.get('/seller/sendDing',seller.sendDing);



//*************买家**************

let store = multer.diskStorage({
    destination: 'public/images/user',

    filename: (req, file, cb) =>{


        cb(null,'buller'+req.session.buller._id +'.jpg');
        req.body.avatar ='/images/user/buller'+ req.session.buller._id +'.jpg';

    }
});
let upl2 = multer({
    storage: store
});

router.post('/buller/regist',buller.regist);
router.post('/buller/login',buller.login);
router.get('/buller/checkLogin',buller.checkLogin);
router.get('/buller/out',buller.out);
router.post('/buller/changeInfo',upl2.single('photo'),buller.changeInfo);
router.get('/buller/goodsList',buller.goodsList);
router.post('/buller/search',buller.search);
router.get('/buller/checkSearch',buller.checkSearch);
router.post('/buller/addArea',buller.addArea);
router.get('/buller/removeArea',buller.removeArea);
router.get('/buller/checkFollow',buller.checkFollow);
router.get('/buller/myFollowed',buller.myFollow);
router.get('/buller/myLikes',buller.myLikes);
//购物链
router.post('/buller/dingArray',buller.dingArray);
router.post('/buller/dingOne',buller.dingOne);
router.get('/buller/bullSuccess',buller.bullSuccess);
router.get('/buller/noBull',buller.noBull);
router.get('/buller/noSend',buller.noSend);
router.get('/buller/noGet',buller.noGet);
router.get('/buller/historyDing',buller.historyDing);
router.get('/buller/dingInfo',buller.dingInfo);
router.get('/buller/toBull',buller.toBull);
router.get('/buller/getDing',buller.getDing);
router.get('/buller/removeDing',buller.removeDing);
router.get('/buller/removeHistoryDing',buller.removeHistoryDing);

//***********商品功能***********

let storage3 = multer.diskStorage({
    destination: 'public/images/desc',

    filename: (req, file, cb) =>{
    if(!req.body.xu){
        req.body.xu = 1;
    }
        if ( req.body.xu instanceof Array){
            req.body.xu = req.body.xu[0]-0;
        }else{
            req.body.xu = req.body.xu-0;
        }
        var a = Date.now();


        var ava = '/images/desc/'+req.session.buller._id + '-' +(req.body.xu++)+'-'+ a +'.jpg,';

        cb(null,req.session.buller._id + '-' +(req.body.xu-1)+'-'+ a +'.jpg');
        req.body.avatar += ava;

    }
});
let upl3 = multer({
    storage: storage3
});

router.get('/buller/goodInfo/:id',good.goodInfo)
router.post('/buller/plusCar',good.plusCar);
router.get('/buller/editCar',good.editCar);
router.get('/buller/store',good.store);
router.get('/buller/checkStore',good.checkStore);
router.post('/buller/desc',upl3.array('photo',10),good.desc);
router.get('/buller/getDesc',good.getDescs);
router.post('/buller/reply',good.reply);
router.get('/buller/getReply',good.getReplys);


// **************消息功能***************
router.get('/buller/getMsgs',message.getMsgs);
router.get('/seller/getMsgs',message.getSellerMsgs);
router.get('/buller/readMsg',message.readMsg);
router.get('/buller/removeMsg',message.removeMsg);
router.get('/buller/readSellerMsg',message.readSellerMsg);
router.get('/buller/removeSellerMsg',message.removeSellerMsg);

//*************超管**************
router.get('/',home.index);

module.exports = router;
